package com.will.txj.aj.security.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * @author: wen-yi;
 * @date: 2021/12/5 10:57;
 * @Description: 编码拦截器
 */
@Slf4j
@Component
public class CharacterInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 该方法在action执行前执行，可以实现对数据的预处理，比如：编码、安全控制等
        //request.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        //response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        //log.info("设置编码格式: utf-8");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // Controller处理后执行 此时还未视图渲染 | 有机会修改视图层数据
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 视图渲染 之后执行 | 最后执行，通常用于释放资源，处理异常
    }
}
